package com.moguhoutai.biz;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.moguhoutai.dao.LandlordMapper;
import com.moguhoutai.dao.ResourceMapper;
import com.moguhoutai.model.Landlord;
import com.moguhoutai.util.BusinessException;
@Service
public class LandlordBiz {

	    @Resource
		private LandlordMapper lMapperdao;
	    
	    @Resource
		private ResourceMapper reMapperdao;
	
	
	    public Landlord findById(int landId) throws BusinessException {
			try {
				return lMapperdao.selectByPrimaryKey(landId);
			} catch (Exception e) {
				e.printStackTrace();
				throw new BusinessException("E03B05");
			}
		}
		
	    public void delete(Landlord landlord) throws BusinessException {
			try {
				lMapperdao.deleteByPrimaryKey(landlord.getLandId());
			} catch (Exception e) {
				e.printStackTrace();
				throw new BusinessException("E03B02");
			}
		}
	    
	    public void save(Landlord landlord)  {
	  		try {
	  			Date now = new Date();
	  			landlord.getRes().setCreateTime(now);
	  			landlord.getRes().setUpdateTime(now);
	  			reMapperdao.insert(landlord.getRes());
	  			landlord.setCreateTime(now);
	  			lMapperdao.insert(landlord);
	  		} catch (Exception e) {
	  			e.printStackTrace();
	  			
	  		}
	  	}
	    
	    
	    public List<Landlord> findAll() throws BusinessException {
			try {
				return lMapperdao.findAll();
			} catch (Exception e) {
				e.printStackTrace();
				throw new BusinessException("E02B04");
			}
		}
		public long findCount() throws BusinessException {
			try {
				return lMapperdao.findCount();
			} catch (Exception e) {
				e.printStackTrace();
				throw new BusinessException("E01B06");
			}
		}
	    
		public void update(Landlord landlord) throws BusinessException {
			try {
				Date now = new Date();
				if(landlord.getRes() != null){
					landlord.getRes().setCreateTime(now);
					landlord.getRes().setUpdateTime(now);
					reMapperdao.insert(landlord.getRes());
				}
				landlord.setCreateTime(now);
				lMapperdao.updateByPrimaryKeySelective(landlord);
			} catch (Exception e) {
				e.printStackTrace();
				throw new BusinessException("E04B03");
			}
		}
		
		
}
